﻿namespace WMS6.Web.Configuration
{
    using System;
    using System.Collections.Generic;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;
    using WMS6.Web;
    using WMS6.Web.BaseInfoServiceRef;

    public class OutboundValidationRule : BasePage
    {
        protected Button btnAddDetail;
        protected Button btnCopy;
        protected Button btnDelete;
        protected Button btnDeleteDetail;
        protected Button btnGoBack;
        protected Button btnNew;
        protected Button btnSave;
        protected CheckBox chkIsActive;
        protected CheckBox chkSysCreated;
        protected DropDownList ddlType;
        protected HtmlForm F2206;
        protected GridView gvOutboundValidationRuleDetail;
        protected HtmlInputHidden hdnFlag;
        protected HtmlInputHidden hdnFrom;
        protected HtmlInputHidden hdnSeqNo;
        protected HtmlInputHidden hdnValidationRuleID;
        protected HtmlImage img1;
        protected HtmlImage imgOwnerID;
        protected Label lblArea;
        protected Label lblCustomerID;
        protected Label lblMinSafeDays;
        protected Label lblMsg;
        protected Label lblOutboundValidationRuleID;
        protected Label lblOwnerID;
        protected Label lblPutawayRuleDescr;
        protected Label lblSkuID;
        protected Label lblType;
        protected HtmlLink mycss;
        protected Label one1;
        protected Label one2;
        protected ScriptManager ScriptManager1;
        protected TextBox txtCustomerID;
        protected TextBox txtMinSafeDays;
        protected TextBox txtOutboundValidationID;
        protected TextBox txtOwnerID;
        protected TextBox txtPutawayRuleDescr;
        protected TextBox txtSkuID;
        protected UpdatePanel up1;

        protected void btnAddDetail_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(this.hdnValidationRuleID.Value))
            {
                this.btnSave_Click(null, null);
            }
            base.Response.Redirect(string.Format("OutboundValidationRuleDetail.aspx?from={0}&ID={1}&SeqNO={2}", base.UrlEncode(this.hdnFrom.Value), base.UrlEncode(this.hdnValidationRuleID.Value), base.UrlEncode(this.hdnSeqNo.Value)), true);
        }

        protected void btnCopy_Click(object sender, EventArgs e)
        {
            this.txtOutboundValidationID.Text = "";
            this.hdnValidationRuleID.Value = "";
            this.txtOutboundValidationID.Enabled = true;
            this.chkIsActive.Checked = true;
            base.DataEntityViewState = null;
        }

        protected void btnDelete_Click(object sender, EventArgs e)
        {
            try
            {
                if (!string.IsNullOrEmpty(this.txtOutboundValidationID.Text))
                {
                    List<string> validationID = new List<string> {
                        this.txtOutboundValidationID.Text.Trim()
                    };
                    this.lblMsg.Text = BaseInfoServiceClientProxy.GetInstance().Delete(validationID, "OUT_BOUND_VALIDATION") + base.GetGlobalMsgResource("C006");
                    this.btnSave.Enabled = false;
                    this.btnDelete.Enabled = false;
                    this.btnCopy.Enabled = false;
                    base.DataEntityViewState = null;
                }
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = base.GetGlobalMsgResource("C007") + ex.Message;
            }
        }

        protected void btnDeleteDetail_Click(object sender, EventArgs e)
        {
            try
            {
                if (!string.IsNullOrEmpty(this.txtOutboundValidationID.Text))
                {
                    List<string> validationID = new List<string> {
                        this.txtOutboundValidationID.Text.Trim()
                    };
                    List<string> SeqNo = base.GetSelectedIDsInGridView(this.gvOutboundValidationRuleDetail, "SeqNo");
                    if ((SeqNo == null) || (SeqNo.Count == 0))
                    {
                        this.lblMsg.Text = base.GetGlobalMsgResource("C008");
                    }
                    else
                    {
                        this.lblMsg.Text = BaseInfoDetailServiceClientProxy.GetInstance().DeleteDetail(validationID, SeqNo, new List<string>(), "OUT_BOUND_VALIDATION_DETAIL") + base.GetGlobalMsgResource("C006");
                        this.SetOutboundValidationInfo();
                        this.btnSave.Enabled = false;
                        this.btnDelete.Enabled = false;
                        this.btnCopy.Enabled = false;
                    }
                }
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = base.GetGlobalMsgResource("C007") + ex.Message;
            }
        }

        protected void btnGoBack_Click(object sender, EventArgs e)
        {
            switch (this.hdnFrom.Value)
            {
                case "query":
                    base.Response.Redirect("OutboundValidationRuleQuery.aspx", false);
                    break;

                case "list":
                    base.Response.Redirect("OutboundValidationRuleList.aspx?from=edit", false);
                    break;
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(this.txtOutboundValidationID.Text))
            {
                this.lblMsg.Text = "请输入出库验证规则代码!";
            }
            else if (string.IsNullOrEmpty(this.txtSkuID.Text))
            {
                this.lblMsg.Text = "请输入货品!";
            }
            else if (string.IsNullOrEmpty(this.txtOwnerID.Text))
            {
                this.lblMsg.Text = "请输入货主!";
            }
            else if (string.IsNullOrEmpty(this.txtCustomerID.Text))
            {
                this.lblMsg.Text = "请输入客户!";
            }
            else if (base.DataEntityViewState != null)
            {
                this.UpdateOutboundValidationInfo();
            }
            else
            {
                this.InsertOutboundValidationInfo();
            }
        }

        private OutboundValidationInfo GetOutboundValidationInfo(OutboundValidationInfo outboundValidationInfo)
        {
            outboundValidationInfo.ConsigneeID = this.txtCustomerID.Text;
            outboundValidationInfo.Type = this.ddlType.SelectedValue;
            outboundValidationInfo.MinimumShelfLife = !string.IsNullOrEmpty(this.txtMinSafeDays.Text) ? int.Parse(this.txtMinSafeDays.Text) : 0;
            outboundValidationInfo.ValidationID = this.txtOutboundValidationID.Text;
            this.txtOutboundValidationID.Enabled = false;
            outboundValidationInfo.OwnerID = this.txtOwnerID.Text;
            outboundValidationInfo.Descr = this.txtPutawayRuleDescr.Text;
            outboundValidationInfo.SkuID = this.txtSkuID.Text;
            outboundValidationInfo.IsDefault = "Y";
            outboundValidationInfo.IsActive = this.chkIsActive.Checked ? "Y" : "N";
            outboundValidationInfo.SystemCreated = this.chkSysCreated.Checked ? "Y" : "N";
            return outboundValidationInfo;
        }

        protected void gvOutboundValidationRuleDetail_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                e.Row.Cells[0].Text = (e.Row.RowIndex + 1).ToString();
                e.Row.Attributes.Add("onmouseout", "GridViewRowMouseout(this);");
                e.Row.Attributes.Add("onmouseover", "GridViewRowMouseover(this);");
                ((CheckBox) e.Row.FindControl("chbSelect")).Attributes.Add("onclick", "GridViewCheckBoxClick('" + e.Row.ClientID + "')");
            }
        }

        private void InsertOutboundValidationInfo()
        {
            try
            {
                OutboundValidationInfo outboundValidationInfo = new OutboundValidationInfo();
                outboundValidationInfo = this.GetOutboundValidationInfo(outboundValidationInfo);
                outboundValidationInfo.CreatedBy = base.UserInfo.UserID;
                this.lblMsg.Text = BaseInfoServiceClientProxy.GetInstance().Insert(outboundValidationInfo, "OUT_BOUND_VALIDATION") + base.GetGlobalMsgResource("C004");
                base.DataEntityViewState = outboundValidationInfo;
                this.txtOutboundValidationID.Enabled = false;
                this.hdnValidationRuleID.Value = this.txtOutboundValidationID.Text;
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = base.GetGlobalMsgResource("C005") + ex.Message;
            }
        }

        private void LoadData()
        {
            base.BindData2DDLBySysCode(this.ddlType, "OTValidationRuleType");
            if (!string.IsNullOrEmpty(base.Request.QueryString["ID"]))
            {
                this.hdnValidationRuleID.Value = base.Request.QueryString["ID"];
                this.SetOutboundValidationInfo();
            }
            else
            {
                this.hdnSeqNo.Value = "10";
            }
            if (!string.IsNullOrEmpty(base.Request.QueryString["from"]))
            {
                this.hdnFrom.Value = base.Request.QueryString["from"].ToString();
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!base.IsPostBack)
            {
                base.InitPage(this.Page);
                this.LoadData();
            }
            this.lblMsg.Text = "";
        }

        private void SetOutboundValidationInfo()
        {
            try
            {
                string ValidationID = this.hdnValidationRuleID.Value;
                OutboundValidationInfo outboundValidationInfo = BaseInfoServiceClientProxy.GetInstance().GetInfoByID(ValidationID, "OUT_BOUND_VALIDATION") as OutboundValidationInfo;
                base.DataEntityViewState = outboundValidationInfo;
                if (outboundValidationInfo != null)
                {
                    this.txtCustomerID.Text = outboundValidationInfo.ConsigneeID;
                    this.txtMinSafeDays.Text = outboundValidationInfo.MinimumShelfLife.ToString();
                    this.ddlType.SelectedValue = outboundValidationInfo.Type;
                    this.txtOutboundValidationID.Text = outboundValidationInfo.ValidationID;
                    this.txtOutboundValidationID.Enabled = false;
                    this.txtOwnerID.Text = outboundValidationInfo.OwnerID;
                    this.txtPutawayRuleDescr.Text = outboundValidationInfo.Descr;
                    this.txtSkuID.Text = outboundValidationInfo.SkuID;
                    this.chkIsActive.Checked = outboundValidationInfo.IsActive == "Y";
                    this.chkSysCreated.Checked = outboundValidationInfo.SystemCreated == "Y";
                    base.BindData2GridView(this.gvOutboundValidationRuleDetail, outboundValidationInfo.OutboundValidationDetailInfos);
                    if ((outboundValidationInfo.OutboundValidationDetailInfos != null) && (outboundValidationInfo.OutboundValidationDetailInfos.Count > 0))
                    {
                        this.hdnSeqNo.Value = (outboundValidationInfo.OutboundValidationDetailInfos[outboundValidationInfo.OutboundValidationDetailInfos.Count - 1].SeqNo + 10).ToString();
                    }
                    else
                    {
                        this.hdnSeqNo.Value = "10";
                    }
                }
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = ex.Message;
            }
        }

        private void UpdateOutboundValidationInfo()
        {
            try
            {
                OutboundValidationInfo outboundValidationInfo = base.DataEntityViewState as OutboundValidationInfo;
                outboundValidationInfo = this.GetOutboundValidationInfo(outboundValidationInfo);
                outboundValidationInfo.UpdatedBy = base.UserInfo.UserID;
                this.lblMsg.Text = BaseInfoServiceClientProxy.GetInstance().Update(outboundValidationInfo, "OUT_BOUND_VALIDATION") + base.GetGlobalMsgResource("C002");
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = "出库验证规则更新异常" + ex.Message;
            }
        }
    }
}

